// pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperIndex: 0,
    paginationState: false,
    tabsIndex: 0,
    upState: false,
    ShowPagination: false,
    loading: true,

    banners: [{
        type: "video",
        url: "http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400",
        poster: "/image/Rectangle115.png",
        thumbnail: "/image/Rectangle1374.jpg",
        state: false,
        currentTime: 0
      },
      {
        type: "image",
        url: "/image/Rectangle1374-1.jpg",
        thumbnail: "/image/Rectangle1375.jpg"
      },
      {
        type: "image",
        url: "/image/87193c16b16a6edc52f11483d2f9e20c.jpg",
        thumbnail: "/image/Rectangle1376.jpg"
      },
      {
        type: "image",
        url: "/image/4272dcd3244ad91753b77682c0c7ed6a.jpg",
        thumbnail: "/image/Rectangle115.png"
      }
    ],

    options: [{
      name: "一件 (10包)",
      unit: "¥6.54/斤",
      price: 133.97,
      batch: [{
        name: "每满5包减4.1 (¥40/包)",
        quantity: 5
      }],
      quantity: 0
    }, {
      name: "一件 (20包)",
      unit: "¥6.35/斤",
      price: 146.72,
      discount: 159.67,
      batch: [{
        name: "每满5包减4.1 (¥40/包)",
        quantity: 5
      }, {
        name: "每满10包减6.1 (¥38/包)",
        quantity: 10
      }],
      quantity: 0
    }],

    tabs: [{
      title: "推荐加购",
      products: [{
        image: "/image/Rectangle1227.jpg",
        name: "[新西兰星牌] 牛肚片牛肚片",
        tags: ["特价", "限时折扣", "领¥30.88红包"],
        price: 2533.97,
        unit: "/包",
        cart_quantity: 99
      }, {
        image: "/image/d905729f02ed25af9d7d629e32efda94.jpg",
        name: "[长江桂柳] 大白条鸭(5.0斤*6只) 15kg15kg15kg15kg15kg...",
        tags: ["满赠"],
        price: 71.18,
        unit: "/斤",
        cart_quantity: 9
      }, {
        image: "/image/Rectangle1228.jpg",
        name: "[九帝] 肉排粒/排骨",
        tags: [],
        price: 14.45,
        unit: "/斤",
        cart_quantity: 0
      }]
    }, {
      title: "相似商品",
      products: [{
        image: "/image/d905729f02ed25af9d7d629e32efda94.jpg",
        name: "[长江桂柳] 大白条鸭(5.0斤*6只) 15kg15kg15kg15kg15kg...",
        tags: ["满赠"],
        price: 71.18,
        unit: "/斤",
        cart_quantity: 9
      }, {
        image: "/image/Rectangle1227.jpg",
        name: "[新西兰星牌] 牛肚片牛肚片",
        tags: ["特价", "限时折扣", "领¥30.88红包"],
        price: 2533.97,
        unit: "/包",
        cart_quantity: 99
      }, {
        image: "/image/Rectangle1228.jpg",
        name: "[九帝] 肉排粒/排骨",
        tags: [],
        price: 14.45,
        unit: "/斤",
        cart_quantity: 0
      }]
    }, ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    setTimeout(() => {
      this.setData({
        loading: false
      })
    }, 1500);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  previewMedia() {
    wx.previewMedia({
      sources: this.data.banners
    })
  },

  tabChange(e) {
    this.setData({
      tabsIndex: e.currentTarget.dataset.index
    })
  },

  optionsChange(e) {
    let index = e.currentTarget.dataset.index
    let variable = parseInt(e.currentTarget.dataset.variable)
    if (this.data.options[index].quantity == 0 && variable < 0) {
      variable = 0
    }
    this.setData({
      ["options[" + index + "].quantity"]: this.data.options[index].quantity + variable
    })
    wx.vibrateShort({
      type: "heavy"
    })
  },

  swiperChange(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },

  setSwiperIndex(e) {
    this.setData({
      swiperIndex: e.currentTarget.dataset.current
    })
  },

  pagination(e) {
    this.setData({
      paginationState: !this.data.paginationState
    })
  },

  videoPlay(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      ["banners[" + index + "].state"]: !this.data.banners[index].state
    })
    const video = wx.createVideoContext("myVideo" + index)
    if (this.data.banners[index].state) {
      video.play()
      this.setData({
        ShowPagination: true
      })
    } else {
      video.stop()
      this.setData({
        ShowPagination: false
      })
    }
  },

  videoTimeUpdate(e) {
    let index = e.currentTarget.dataset.index
    let currentTime = e.detail.currentTime
    if (currentTime == e.detail.duration) {
      currentTime = 0
      this.setData({
        ["banners[" + index + "].state"]: false
      })
    }
    this.setData({
      ["banners[" + index + "].currentTime"]: currentTime
    })
  },

  up() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 500
    })
  },

  onPageScroll(e) {
    if (e.scrollTop > 300) {
      this.setData({
        upState: true
      })
    } else {
      this.setData({
        upState: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})